package builder;


import mapping.Configuration;
import org.dom4j.Element;

import java.util.List;

/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2020-01-08 11:13
 * @notify 解析mapper文件获取namespace，读取到<insert>和<select>标签，交给XMLStatementBuilder
 * @version 1.0
 */
public class XMLMapperBuilder {

    private String namespace = "";

    private Configuration configuration;

    public XMLMapperBuilder(Configuration configuration) {
        this.configuration = configuration;
    }

    //解析每一个mapper文件
    public void parse(Element rootElement) throws Exception {
        //查询namespace
        namespace = rootElement.attributeValue("namespace");
        //获取select标签
        List<Element> selectElements = rootElement.elements("select");
        parse(selectElements, "select");

        List<Element> insertElements = rootElement.elements("insert");
        parse(insertElements, "insert");

    }

    public void parse(List<Element> selectElements, String sqlType) throws Exception {
        for (Element selectElement : selectElements) {
            XMLStatementBuilder xmlStatementBuilder = new XMLStatementBuilder(configuration);
            xmlStatementBuilder.parseStatementElement(selectElement, namespace, sqlType);
        }
    }


}
